<?php

namespace Kunfeng\Dify\Callback;

use Kunfeng\Dify\Event\MessageEvent;
use Kunfeng\Dify\Event\MessageEndEvent;
use Kunfeng\Dify\Event\TtsMessageEvent;
use Kunfeng\Dify\Event\TtsMessageEndEvent;
use Kunfeng\Dify\Event\MessageReplaceEvent;
use Kunfeng\Dify\Event\ErrorEvent;
use Kunfeng\Dify\Event\PingEvent;

/**
 * 文本生成流式回调接口
 */
class CompletionStreamCallback
{
    private $handlers = [];

    public function __construct(array $handlers = [])
    {
        $this->handlers = $handlers;
    }

    /**
     * 设置消息处理器
     */
    public function onMessage(callable $handler): self
    {
        $this->handlers['onMessage'] = $handler;
        return $this;
    }

    /**
     * 设置消息结束处理器
     */
    public function onMessageEnd(callable $handler): self
    {
        $this->handlers['onMessageEnd'] = $handler;
        return $this;
    }

    /**
     * 设置TTS消息处理器
     */
    public function onTtsMessage(callable $handler): self
    {
        $this->handlers['onTtsMessage'] = $handler;
        return $this;
    }

    /**
     * 设置TTS消息结束处理器
     */
    public function onTtsMessageEnd(callable $handler): self
    {
        $this->handlers['onTtsMessageEnd'] = $handler;
        return $this;
    }

    /**
     * 设置消息替换处理器
     */
    public function onMessageReplace(callable $handler): self
    {
        $this->handlers['onMessageReplace'] = $handler;
        return $this;
    }

    /**
     * 设置错误处理器
     */
    public function onError(callable $handler): self
    {
        $this->handlers['onError'] = $handler;
        return $this;
    }

    /**
     * 设置异常处理器
     */
    public function onException(callable $handler): self
    {
        $this->handlers['onException'] = $handler;
        return $this;
    }

    /**
     * 设置心跳处理器
     */
    public function onPing(callable $handler): self
    {
        $this->handlers['onPing'] = $handler;
        return $this;
    }

    /**
     * 触发事件
     */
    public function trigger(string $event, ...$args): void
    {
        if (isset($this->handlers[$event])) {
            ($this->handlers[$event])(...$args);
        }
    }

    /**
     * 处理消息事件
     */
    public function handleMessage(MessageEvent $event): void
    {
        $this->trigger('onMessage', $event);
    }

    /**
     * 处理消息结束事件
     */
    public function handleMessageEnd(MessageEndEvent $event): void
    {
        $this->trigger('onMessageEnd', $event);
    }

    /**
     * 处理TTS消息事件
     */
    public function handleTtsMessage(TtsMessageEvent $event): void
    {
        $this->trigger('onTtsMessage', $event);
    }

    /**
     * 处理TTS消息结束事件
     */
    public function handleTtsMessageEnd(TtsMessageEndEvent $event): void
    {
        $this->trigger('onTtsMessageEnd', $event);
    }

    /**
     * 处理消息替换事件
     */
    public function handleMessageReplace(MessageReplaceEvent $event): void
    {
        $this->trigger('onMessageReplace', $event);
    }

    /**
     * 处理错误事件
     */
    public function handleError(ErrorEvent $event): void
    {
        $this->trigger('onError', $event);
    }

    /**
     * 处理异常
     */
    public function handleException(\Throwable $exception): void
    {
        $this->trigger('onException', $exception);
    }

    /**
     * 处理心跳事件
     */
    public function handlePing(PingEvent $event): void
    {
        $this->trigger('onPing', $event);
    }
}
